package com.googlecode.dgwt.client.dijit.base.state;

import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.dgwt.client.dijit.Dijit;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class ChildrenState extends WidgetState {

    private static ChildrenState instance;
      
    public static ChildrenState get() {
        if (instance == null) {
            instance = new ChildrenState();
        }
        return instance;
    }
    
    
    private ChildrenState() {
    }
    
    @Override
    protected String getDependentStyleClass() {
        return Dijit.getTheme().getChildrenName();
    }

    @Override
    public void update(Widget widget, boolean activate) {
        String separator = Dijit.getTheme().getDependentSeparator();
        String style = widget.getStylePrimaryName() + separator + this.getDependentStyleClass();
        if (widget instanceof HasWidgets){
            for (Widget child : (HasWidgets) widget){
                if (activate) {
                    child.addStyleName(style);
                } else {
                    child.removeStyleName(style);
                }
            }
        }
    }
    
    public void updateChild(Widget widget, Widget child, boolean activate) {
        String separator = Dijit.getTheme().getDependentSeparator();
        String style = widget.getStylePrimaryName() + separator + this.getDependentStyleClass();
        if (activate) {
            child.addStyleName(style);
        } else {
            child.removeStyleName(style);
        }
    }

}
